package com.byhealth.act.base.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.SimpleTimeZone;

public class DateUtil {

	public static final String SIMPLE_PATTERN = "yyyy-MM-dd";
	public static final String FULL_PATTERN = "yyyy-MM-dd HH:mm:ss";
	
	public static String getFormatString(Date currentDate,String pattern){
		if(currentDate == null) {
			
			return "";
		}else {
			SimpleDateFormat dateformat =  new SimpleDateFormat(pattern);
			return dateformat==null?"":dateformat.format(currentDate);
		}
	}
	
	
	public static Date parseToDate(String dateStr) throws ParseException {
		return parseToDate(dateStr, FULL_PATTERN);
	}
	
	public static Date parseToDate(String dateStr, String pattern) {
		String orginalPattern = pattern;
		if(StringUtil.isEmpty(dateStr)) {
			return new Date();
		}
		if(StringUtil.isEmpty(pattern)) {
			pattern = SIMPLE_PATTERN;
		}
		Date date = null;
		SimpleDateFormat format = new SimpleDateFormat();
		try {
			format.applyPattern(pattern);
			date = format.parse(dateStr);
		} catch (ParseException e) {
			//nothing to do
		}
		if(date == null) {
			if(FULL_PATTERN.equals(pattern)) {
				format.applyPattern(SIMPLE_PATTERN);
			} else {
				format.applyPattern(FULL_PATTERN);
			}
			try {
				date = format.parse(dateStr);
			} catch (ParseException e) {
				throw new UnsupportedOperationException("DateUtil doesn't support the date pattern : " + orginalPattern);
			}
		}
		return date;
	}
	
	public static String format(Date date) {
		return format(date, SIMPLE_PATTERN);
	}

	public static String format(Date date, String pattern) {
		if(date == null) {
			return "";
		}
		if(StringUtil.isEmpty(pattern)) {
			pattern = SIMPLE_PATTERN;
		}
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}
	
	public static Date getCurrentDate() {
		return new Date();
	}
	
	/**
	 * 获取当前时间正负N天后的时间
	 * @Author:laigengbiao
	 * @Description: 
	 * @param days 正数为前推N天，负数为后推N天
	 * @return   
	 * @throws
	 */
	public static Date addDateByDays(int days){
		return addDateByDays(getCurrentDate(),days);
	}
	/**
	 * 获取指定时间的正负N天后的时间
	 * @Author:laigengbiao
	 * @Description: 
	 * @param source	指定时间
	 * @param days		正数为前推N天，负数为后推N天
	 * @return   
	 * @throws
	 */
	public static Date addDateByDays(Date source, int days){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(source);
		calendar.add(Calendar.DATE, days);
		return calendar.getTime();
	}
	
	/**
	 * 获取当前时间正负N月后的时间
	 * @Author:laigengbiao
	 * @Description: 
	 * @param months	正数为前推N月，负数为后推N月
	 * @return   
	 * @throws
	 */
	public static Date addDateByMonths(int months){
		return addDateByMonths(new Date(),months);
	}
	/**
	 * 获取指定时间的正负N月后的时间
	 * @Author:laigengbiao
	 * @Description: 
	 * @param source	指定时间
	 * @param days		正数为前推N月，负数为后推N月
	 * @return   
	 * @throws
	 */
	public static Date addDateByMonths(Date source, int months){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(source);
		calendar.add(Calendar.MONTH, months);
		return calendar.getTime();
	}
	/**
	 * @throws ParseException 
	 * 获取短日期（2012-12-21）
	 * @Author:laigengbiao
	 * @Description: 
	 * @param date
	 * @return   
	 * @throws
	 */
	public static Date getShortDate(Date date) throws ParseException {
		return parseToDate(format(date, SIMPLE_PATTERN)); 
	}
	
	public static String[] getPreviousNDays(int n) {
		String[] days = new String[Math.abs(n)];
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		
		int i = 0;
		
		for (int j = n + 1; j <= -1; j++) {
			Calendar calendar = Calendar.getInstance();
			calendar.add(Calendar.DATE, j);
			
			days[i] = df.format(calendar.getTime());
			
			i++;
		}
		
		days[days.length - 1] = df.format(new Date());
		
		return days;
	}
	
	public static Date strToDate(String input, String format) {
		try {
			SimpleDateFormat df = new SimpleDateFormat(format);
			
			return df.parse(input);
		} catch (Exception e) {
			return null;
		}
	}
	
	/**
	 * 获得Year年份列表。根据参数增量来，正整数获取从今年起(包括今年)后面多少年，附整数从今年(包括今年)算起的前多少年。
	 * 
	 * @param increment
	 * @return
	 */
	public static List<Integer> getYearList(int increment, boolean isContainThisYear){
		
		List<Integer> result = new ArrayList<Integer>();

		int thisYear = Calendar.getInstance().get(Calendar.YEAR);
		
		if(isContainThisYear){
			result.add(thisYear);
		}else{
			increment = increment > 0 ? increment + 1 : increment - 1; 
		}
			
		
		if(increment > 0 ){
			
			for(int x = 1; x < increment; x++){
				result.add(++thisYear);
			}
			
		}else{
			
			for(int x = -1; x > increment; x--){
				result.add(--thisYear);
			}
			
		}
		
		
		return result;
		
	}
	
	/**
	 * 获得newDate - oldDate的时间差，单位为秒
	 * @param newDate
	 * @param oldDate
	 * @return
	 */
	public static int getTimeDifference(Date newDate,Date oldDate){
		long timeDifference = newDate.getTime() - oldDate.getTime();
		return (int)(timeDifference/1000);
	}
	
	
	/**
	 * 获取指定时间的正负N小时后的时间
	 * @Author:zl
	 * @Description: 
	 * @param source	指定时间
	 * @param hours		正数为前推N小时，负数为后推N小时
	 * @return   
	 * @throws
	 */
	public static Date addDateByHours(Date source, int hours){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(source);
		calendar.add(Calendar.HOUR_OF_DAY, hours);
		return calendar.getTime();
	}
	
	/*public static void main(String[] args) throws Exception {
		String[] days = getPreviousNDays(-3);
		
		for (String day : days) {
			System.out.println(day);
		}
	}*/
	
	/**
	 * 将date转化成UTC格式
	 * @param date yyyy-MM-dd HH:mm:ss
	 * @return
	 * liangjinquan
	 */
	public static String changeToMongoDate(Date date)
	{
		try {
			SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
			sdf.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
			return sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 将微信消息中的CreateTime转换成标准格式的时间（yyyy-MM-dd HH:mm:ss）
	 * CreateTime，表示1970年1月1日0时0分0秒至消息创建时所间隔的秒数，不是毫秒数！
	 * @param createTime 消息创建时间
	 * @return
	 */
	public static String formatCreateTimeString(String createTime) {
		// 将微信传入的CreateTime转换成long类型，再乘以1000
		long msgCreateTime = Long.parseLong(createTime) * 1000L;
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(new Date(msgCreateTime));
	}
	
	/**
	 * 将微信消息中的CreateTime转换成标准格式的时间（yyyy-MM-dd HH:mm:ss）
	 * CreateTime，表示1970年1月1日0时0分0秒至消息创建时所间隔的秒数，不是毫秒数！
	 * @param createTime 消息创建时间
	 * @return
	 */
	public static Date formatCreateTime(String createTime) {
		// 将微信传入的CreateTime转换成long类型，再乘以1000
		long msgCreateTime = Long.parseLong(createTime) * 1000L;
		return new Date(msgCreateTime);
	}
	
	/** 获取下个月第一天的0时0分0秒 */
	public static Date getNextMontthBeginTime() {
		try {
			 Calendar cal = Calendar.getInstance();    
			 cal.set(Calendar.DATE, cal.getActualMaximum(Calendar.DATE)); 
			 cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
			 cal.add(Calendar.DAY_OF_MONTH,1);		
			 return cal.getTime();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/** 获取本月第一天的0时0分0秒 */
	public static Date getCurrMontthBeginTime() {
		try {
			 Calendar cal = Calendar.getInstance();    
			 cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.getActualMinimum(Calendar.DAY_OF_MONTH), 0, 0, 0);
			 return cal.getTime();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	

	/**
	 * 获取指定时间的正负N分钟后的时间
	 * @Description: 
	 * @param source	指定时间
	 * @param hours		正数为前推N分钟，负数为后推N分钟
	 * @return   
	 * @throws
	 */
	public static Date addDateByMinutes(Date source, int minutes){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(source);
		calendar.add(Calendar.MINUTE, minutes);
		return calendar.getTime();
	}
	
	
	/**
	 * 比较日期大小
	 * @param srcDate		
	 * @param targetDate
	 * @param format
	 * @return
	 */
	public static int compareDate(String srcDate, String targetDate, String format) {
	     try {
	    	 DateFormat df = new SimpleDateFormat(format);
	    	 Date dt1 = df.parse(srcDate);
	         Date dt2 = df.parse(targetDate);
	         if (dt1.getTime() < dt2.getTime()) {
	             return 1;
	         } else if (dt1.getTime() > dt2.getTime()) {
	             return -1;
	         } else {
	             return 0;
	         }
	     } catch (Exception exception) {
	         exception.printStackTrace();
	     }
	     return 0;
	}
	
	
	
	public static int compareDate(String srcDate, Date dt2) {
	     try {
	    	 DateFormat df = new SimpleDateFormat(FULL_PATTERN);
	    	 Date dt1 = df.parse(srcDate);
	         if (dt1.getTime() < dt2.getTime()) {
	             return 1;
	         } else if (dt1.getTime() > dt2.getTime()) {
	             return -1;
	         } else {
	             return 0;
	         }
	     } catch (Exception exception) {
	         exception.printStackTrace();
	     }
	     return 0;
	}

}
